<template>
  <div class="updatePassword">
    <el-form ref="updatePasswordForm" :model="updatePasswordForm" :rules="updatePasswordRules" class="updatePassword-form">
      <h2 class="title">
        校园论坛系统
        <br>
        <p style="font-size: 16px;margin-top: 2px;color: rgb(64, 158, 255);5">修改密码</p>
      </h2>
      <!-- <h4 class="title">修改密码</h4> -->
      <el-form-item prop="email">
        <el-input v-model="updatePasswordForm.email" type="email" auto-complete="off" placeholder="邮箱">
          <template #prefix>
            <el-icon><Suitcase /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="updatePasswordForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleUpdatePassword"
        >
          <template #prefix>
            <el-icon><Message /></el-icon>
          </template>
        </el-input>
        <div class="updatePassword-code">
          <el-button style="width: 100%;height: 100%;" :disabled="isDisable" type="primary" @click="getCode()">{{ disableTime != 0 ? disableTime+'s' : '获取验证码' }}</el-button>
        </div>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="updatePasswordForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleUpdatePassword"
        >
          <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
          v-model="updatePasswordForm.confirmPassword"
          type="password"
          auto-complete="off"
          placeholder="确认密码"
          @keyup.enter.native="handleUpdatePassword"
        >
          <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleUpdatePassword"
        >
          <span v-if="!loading">修改</span>
          <span v-else>修 改 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-updatePassword-footer">
      <span>Copyright © 2024 fanlong All Rights Reserved.  赣ICP备2024040628号-1</span>
    </div>
  </div>
</template>

<script>
import { getEmailCodeImg, updatePassword } from "@/api/login";
import {ElMessage} from 'element-plus';
// import { removeToken } from "@/utils/auth";

export default {
  name: "UpdatePassword",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.updatePasswordForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    const checkoutEmail = (rule, value, callback) => {
      if (!this.validateEmail(value)) {
        callback(new Error("邮箱格式不正确"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      updatePasswordForm: {
        email: "",
        password: "",
        confirmPassword: "",
        code: ""
        // uuid: ""
      },
      updatePasswordRules: {
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        email: [
          { required: true, trigger: "blur", message: "请输入您的邮箱" },
          { required: true, validator: checkoutEmail, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true,
      isDisable: false,
      disableTime: 0,
      interval: null
    };
  },
  created() {
    // this.getCode();
  },
  methods: {
    validateEmail(email) {
      var re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      return re.test(email);
    },
    getCode() {
      if(this.updatePasswordForm.email!=null && this.updatePasswordForm.email!=''){
        // if(this.validateEmail(this.updatePasswordForm.email)){
          var query = {
            email : this.updatePasswordForm.email
          }
          getEmailCodeImg(query).then(res => {
            if(res.code==200){
              this.isDisable = true;
              this.disableTime = 120;
              this.interval = setInterval(() => {
                this.disableTime = this.disableTime-1;
              },1000)
              ElMessage({
                message: res.msg,
                type: 'success'
              });
            }
          });
        // }else{
        //   ElMessage.error("邮箱格式有误，请输入正确邮箱。");
        // }
      }else{
        ElMessage.error("请输入邮箱后，在点击发送验证码。");
      }
    },
    handleUpdatePassword() {
      this.$refs.updatePasswordForm.validate(valid => {
        if (valid) {
          this.loading = true;
          var data = {
            email: this.updatePasswordForm.email,
            password: this.updatePasswordForm.password,
            code: this.updatePasswordForm.code
          }
          updatePassword(data).then(res => {
            if(res.code == 330){
              ElMessage.error(res.msg);
            }else if(res.code == 200){
              ElMessage({
                showClose: true,
                message: "恭喜你，您的账号密码修改成功！",
                type: 'success'
              });
              this.$router.push("/login");
            }
          }).catch(() => {
            this.loading = false;
            ElMessage({
              showClose: true,
              message: "修改密码失败。",
              type: 'warning'
            });
          })
        }
      });
    }
  },
  watch:{
    "disableTime":{
      immediate:true,
      handler(){
        if(this.disableTime==0){
          this.isDisable = false;
          clearInterval(this.interval);
        }
      }
    },
  }
};

</script>

<style rel="stylesheet/scss" lang="scss">
.updatePassword {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url("../assets/images/background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-weight: bolder;
  font-size: 24px;
}
.updatePassword-form {
  border-radius: 6px;
  // background: #ffffff;
  background-color: rgba(255, 255, 255, 0.7);
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.updatePassword-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.updatePassword-code {
  width: 33%;
  height: 38px;
  float: right;
  margin-left: 10px;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-updatePassword-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.updatePassword-code-img {
  height: 38px;
}
</style>
